package com.qinglei.recoup.longconn.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class InstitutionCache {

    private String institutionCacheName = "recoup.radar.institution.name.";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public String getInstitutionName(String snCode){
        String name = (String) redisTemplate.opsForValue().get(institutionCacheName+snCode);
        if(name == null || name ==""){
            String sql = "select t.`name` from t_radar r left  join t_tenant t on r.tenant_id = t.id WHERE  r.is_deleted = 0 and  r.sn_code = '"+ snCode+"'";
            List<Map<String,Object>> names = jdbcTemplate.queryForList(sql);
            if(names==null || names.size()==0){
                name = "未知机构";
            }else{
                name = (String) names.get(0).get("name");
            }


            redisTemplate.opsForValue().set(institutionCacheName+snCode, name, 60*5, TimeUnit.SECONDS);
        }
        return  name;

    }

}
